﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.BLL
{
    /// <summary>
    /// 用户BLL
    /// </summary>
    public partial class PrvUser
    {
        /// <summary>
        /// 用户DAL
        /// </summary>
        private readonly ElingOA6_Admin_DSJCJ.DAL.PrvUser dal = new ElingOA6_Admin_DSJCJ.DAL.PrvUser();
        /// <summary>
        /// 得到一个对象实体,根据用户登陆号、密码
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="loginID">用户登录号</param>
        /// <param name="password">用户密码</param>
        /// <param name="entityNo">组织机构代码</param>
        /// <returns>用户信息</returns>
        public Model.PrvUser GetModelByNameAndPassword(string loginID, string password, string entityNo)
        {
            string newPassword = GetMd5Password(loginID, password, entityNo);
            return dal.GetModelByLoginIDAndPassword(loginID, newPassword);
        }

        /// <summary>
        /// 得到一个对象实体
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fLogID">用户登录号</param>
        /// <param name="fEntityNo">组织机构代码</param>
        /// <returns>用户信息</returns>
        public Model.PrvUser GetModel(string fLogID, string fEntityNo)
        {

            return dal.GetModel(fLogID, fEntityNo);
        }

        /// <summary>
        /// 密码重置
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fLogID">用户登录号</param>
        /// <param name="fpassword">用户密码</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>是否重置成功</returns>
        public bool ResetPwd(string fLogID, string fpassword, string fEntityNo)
        {

            return dal.ResetPwd(fLogID, fpassword, fEntityNo);
        }


        /// <summary>
        /// 删除一条数据
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fLogID">用户登录号</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>是否删除成功</returns>
        public bool Delete(string fLogID, string fEntityNo)
        {

            return dal.Delete(fLogID, fEntityNo);
        }

        /// <summary>
        /// 更新用户
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="userModel">用户信息</param>
        /// <param name="userGroupList">用户组信息</param>
        /// <returns>是否更新成功</returns>
        public string EditPrvUser(Model.PrvUser userModel, List<string> userGroupList)
        {
            return dal.EditPrvUser(userModel, userGroupList);
        }

        /// <summary>
        /// 增加用户
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="userModel">用户信息</param>
        /// <param name="userGroupList">用户组信息</param>
        public void AddPrvUser(Model.PrvUser userModel, List<string> userGroupList)
        {
            dal.AddPrvUser(userModel, userGroupList);
        }

        /// <summary>
        /// 是否存在该记录
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="fLogID">用户登录号</param>
        /// <param name="fEntityNo">组织机构编号</param>
        /// <returns>是否存在</returns>
        public bool Exists(string fLogID, string fEntityNo)
        {
            return dal.Exists(fLogID, fEntityNo);
        }


        /// <summary>
        /// 获取数据和部门编号 FLogID,FUserName,FDeptNo
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="entityNo">组织机构编号</param>
        /// <returns>用户信息列表</returns>
        public List<Model.PrvUser> GetListDept(string entityNo)
        {
            List<Model.PrvUser> list = new List<Model.PrvUser>();
            DataSet ds = dal.GetListDept(entityNo);
            Model.PrvUser model;
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                model = new Model.PrvUser();
                model.FLogID = row["FLogID"].ToString();
                model.FUserName = row["FUserName"].ToString();
                model.FDeptNo = row["FDeptNo"].ToString();
                list.Add(model);
            }

            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="strWhere">SQL查询条件</param>
        /// <returns>返回用户信息列表</returns>
        public List<Model.PrvUser> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="dt">需转换数据源</param>
        /// <returns>转换后数据列表</returns>
        public List<Model.PrvUser> DataTableToList(DataTable dt)
        {
            List<Model.PrvUser> modelList = new List<Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.PrvUser();
                    if (dt.Rows[n]["FLogID"] != null && dt.Rows[n]["FLogID"].ToString() != "")
                    {
                        model.FLogID = dt.Rows[n]["FLogID"].ToString();
                    }
                    if (dt.Rows[n]["FEntityNo"] != null && dt.Rows[n]["FEntityNo"].ToString() != "")
                    {
                        model.FEntityNo = dt.Rows[n]["FEntityNo"].ToString();
                    }
                    if (dt.Rows[n]["FUserName"] != null && dt.Rows[n]["FUserName"].ToString() != "")
                    {
                        model.FUserName = dt.Rows[n]["FUserName"].ToString();
                    }
                    if (dt.Rows[n]["Fpassword"] != null && dt.Rows[n]["Fpassword"].ToString() != "")
                    {
                        model.Fpassword = dt.Rows[n]["Fpassword"].ToString();
                    }
                    if (dt.Rows[n]["FLastLoginTime"] != null && dt.Rows[n]["FLastLoginTime"].ToString() != "")
                    {
                        model.FLastLoginTime = DateTime.Parse(dt.Rows[n]["FLastLoginTime"].ToString());
                    }
                    if (dt.Rows[n]["FLastLoginIPAddr"] != null && dt.Rows[n]["FLastLoginIPAddr"].ToString() != "")
                    {
                        model.FLastLoginIPAddr = dt.Rows[n]["FLastLoginIPAddr"].ToString();
                    }
                    if (dt.Rows[n]["FLoginCnt"] != null && dt.Rows[n]["FLoginCnt"].ToString() != "")
                    {
                        model.FLoginCnt = int.Parse(dt.Rows[n]["FLoginCnt"].ToString());
                    }
                    if (dt.Rows[n]["FProhibitLogin"] != null && dt.Rows[n]["FProhibitLogin"].ToString() != "")
                    {
                        model.FProhibitLogin = int.Parse(dt.Rows[n]["FProhibitLogin"].ToString());
                    }
                    if (dt.Rows[n]["FForeverAvailable"] != null && dt.Rows[n]["FForeverAvailable"].ToString() != "")
                    {
                        model.FForeverAvailable = int.Parse(dt.Rows[n]["FForeverAvailable"].ToString());
                    }
                    if (dt.Rows[n]["FAvailableDate"] != null && dt.Rows[n]["FAvailableDate"].ToString() != "")
                    {
                        model.FAvailableDate = DateTime.Parse(dt.Rows[n]["FAvailableDate"].ToString());
                    }
                    if (dt.Rows[n]["FEmplNo"] != null && dt.Rows[n]["FEmplNo"].ToString() != "")
                    {
                        model.FEmplNo = dt.Rows[n]["FEmplNo"].ToString();
                    }
                    if (dt.Columns.Contains("FSSOUid") && dt.Rows[n]["FSSOUid"] != null && dt.Rows[n]["FSSOUid"].ToString() != "")
                    {
                        model.FSSOUid = dt.Rows[n]["FSSOUid"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 更新用户登录信息
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="loginID">用户登录编号</param>
        /// <param name="lastLoginIpAddr">最后登录IP</param>
        public void UpdateUserLoginMsg(string loginID, string lastLoginIpAddr, string emplNo, string ipAddress)
        {
            dal.UpdateUserLoginMsg(loginID, lastLoginIpAddr, emplNo, ipAddress);
        }

        /// <summary>
        /// 根据公司编号,用户登录名跟密码获取MD5密码
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="loginID">用户登录编号</param>
        /// <param name="password">用户登录密码</param>
        /// <param name="entityNo">组织机构信息</param>
        /// <returns>MD5加密后信息</returns>
        private string GetMd5Password(string loginID, string password, string entityNo)
        {
            string newPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(entityNo + loginID + password, "MD5");
            return newPassword;
        }

        /// <summary>
        /// 修改密码
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="loginID">用户登录编号</param>
        /// <param name="password">用户登录密码</param>
        /// <param name="entityNo">组织机构编号</param>
        public void EditPassword(string loginID, string password, string entityNo)
        {
            string newPassword = GetMd5Password(loginID, password, entityNo);
            dal.EditPassword(loginID, newPassword, entityNo);
        }
        /// <summary>
        /// 根据部门编号获取用户列表
        /// <creator>wangl</creator>
        /// <date>2012-06-11</date>
        /// </summary>
        /// <param name="orgNo"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetPrvUserListByDeptNo(string deptNo, string entityNo)
        {
            DataSet ds = dal.GetPrvUserListByDeptNo(deptNo, entityNo);
            return DataTableToListByTree(ds.Tables[0]);
        }
        /// <summary>
        /// 根据组织编号获取本部门及其子部门的用户列表
        /// <creator>sunlj</creator>
        /// <date>2015-01-30</date>
        /// </summary>
        /// <param name="orgNo"></param>
        /// <returns></returns>
        public List<Model.PrvUser> GetPrvUserListByDeptNoCTE(string deptNo, string entityNo)
        {
            DataSet ds = dal.GetPrvUserListByDeptNoCTE(deptNo, entityNo);
            return DataTableToListByTree(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// <creator>zhongyl</creator>
        /// <date>2013-07-12</date>
        /// </summary>
        /// <param name="dt">需转换数据源</param>
        /// <returns>转换后数据列表</returns>
        public List<Model.PrvUser> DataTableToListByTree(DataTable dt)
        {
            List<Model.PrvUser> modelList = new List<Model.PrvUser>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.PrvUser model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.PrvUser();
                    if (dt.Rows[n]["FLogID"] != null)
                    {
                        model.FLogID = dt.Rows[n]["FLogID"].ToString();
                    }

                    if (dt.Rows[n]["FUserName"] != null)
                    {
                        model.FUserName = dt.Rows[n]["FUserName"].ToString();
                    }
                    if (dt.Rows[n]["FUserName"] != null)
                    {
                        model.FUserName = dt.Rows[n]["FUserName"].ToString();
                    }
                    if (dt.Columns.Contains("FSeq") && dt.Rows[n]["FSeq"] != null && dt.Rows[n]["FSeq"].ToString() != "")
                    {
                        model.FSeq = int.Parse(dt.Rows[n]["FSeq"].ToString());
                    }
                    if (dt.Columns.Contains("FDeptNo") && dt.Rows[n]["FDeptNo"] != null)
                    {
                        model.FDeptNo = dt.Rows[n]["FDeptNo"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }




    }
}
